package com.decent.ejfadmin.finance.service.impl;

import com.decent.ejfadmin.common.bean.MessageBean;
import com.decent.ejfadmin.common.bean.OtherpayConfig;
import com.decent.ejfadmin.common.enums.Consts;
import com.decent.ejfadmin.common.enums.SupRedisKey;
import com.decent.ejfadmin.common.util.Constants;
import com.decent.ejfadmin.common.util.DingTalkMessagePush;
import com.decent.ejfadmin.config.PlatUrlConfig;
import com.decent.ejfadmin.finance.dao.OtherpayConfigDao;
import com.decent.ejfadmin.finance.service.OtherpayConfigService;
import com.decent.ejfadmin.utils.SimpleCacheUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * 代付配置
 *
 * @author weicx
 * @date 2019/02/25
 */
@Service
public class OtherpayConfigServiceImpl implements OtherpayConfigService {
    @Autowired
    private OtherpayConfigDao otherpayConfigDao;
    @Autowired
    private DingTalkMessagePush dingTalkMessagePush;
    @Autowired
    private PlatUrlConfig platUrlConfig;

    /**
     * 查询代付配置
     *
     * @return
     */
    @Override
    public List<OtherpayConfig> queryOtherpayConfig() {
        List<OtherpayConfig> otherpayConfigList = otherpayConfigDao.queryOtherpayConfigs();
        for (OtherpayConfig otherpayConfig : otherpayConfigList) {
            otherpayConfig.setTodayPayMoney(otherpayConfigDao.getOtherpayWithdrawPriceSum(otherpayConfig.getOtherpay()));
        }
        return otherpayConfigList;
    }

    /**
     * 修改限额
     *
     * @param otherpayConfig 代付配置
     * @return
     */
    @Override
    public MessageBean updateOtherpayConfig(OtherpayConfig otherpayConfig) {
        if (otherpayConfigDao.updateOtherpayConfig(otherpayConfig) != 1) {
            return MessageBean.fail();
        }
        return MessageBean.success();
    }

    /**
     * 新增代付配置
     *
     * @param otherpayConfig 代付配置
     * @return
     */
    @Override
    public MessageBean insertOtherpayConfig(OtherpayConfig otherpayConfig) {
        if (otherpayConfigDao.insertOtherpayConfig(otherpayConfig) != 1) {
            return MessageBean.fail();
        }
        return MessageBean.success();
    }

    /**
     * 切换代付
     *
     * @param otherpayConfig 代付配置
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public MessageBean updateOtherpayState(OtherpayConfig otherpayConfig) {
        otherpayConfigDao.updateAllStatus();
        if (otherpayConfigDao.updateStatus(Consts.OPEN, otherpayConfig.getId()) != 1) {
            return MessageBean.fail();
        }
        String contest = "代付提现切换提醒:\n代付提现切换至" + otherpayConfigDao.getOpenOtherpay(otherpayConfig.getId()) + "，请及时加款";
        dingTalkMessagePush.dingDingRobotPushing(contest, platUrlConfig.getAliPaySwitchHookToken());
        return MessageBean.success();
    }

    /**
     * 代付提现开关 true 开启 false 关闭
     *
     * @return
     */
    @Override
    public boolean getOtherpayStatus() {
        Object autoAudit = SimpleCacheUtil.getObject(SupRedisKey.OTHERPAY_WITHDRAW_FLAG.toString());
        if (Objects.isNull(autoAudit)) {
            return true;
        }
        return false;
    }

    /**
     * 代付提现开关 true 开启 false 关闭
     *
     * @param status
     * @return
     */
    @Override
    public MessageBean changeOtherpayStatus(Integer status) {
        if (null == status) {
            return MessageBean.fail("参数有误");
        }
        if (Constants.STATE_OPEN.intValue() == status) {
            SimpleCacheUtil.removeObject(SupRedisKey.OTHERPAY_WITHDRAW_FLAG.toString());
        } else {
            SimpleCacheUtil.setObject(SupRedisKey.OTHERPAY_WITHDRAW_FLAG.toString(), SupRedisKey.OTHERPAY_WITHDRAW_FLAG.toString());
        }
        return MessageBean.success();
    }

}
